/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.sql.*;
import java.util.Calendar;
import java.util.Vector;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException;

import java.util.ArrayList;
import javax.servlet.http.*;
/**
 *
 * @author Sasha Rakic
 */
public class Korisnik {
    private String username;
    private String password;
    private String checkpassword;
    private int kid;
    private String ime;
    private String prezime;
    private String email;
    private String outcome;
    private String funkcija ;
    private int isAdmin;

    public Korisnik(int k, String u,String p,String i,String pp,String e,String f){
        kid=k;
        username=u;
        password=p;
        ime=i;
        prezime=pp;
        email=e;
        funkcija=f;
    }

    public String detalji(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        String vrati;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            ArrayList<Rezervacija> rezervacije=new ArrayList<Rezervacija>();
            rs = stmt.executeQuery("select * from rezervacija where korisnik="+kid+";");
            ArrayList<Integer> list = new ArrayList<Integer>();
            while(rs.next()){
                String sid=rs.getString("sid");
                int ssid=Integer.parseInt(sid);
                //int sid=rs.getInt("sid");
                list.add(ssid);
                rezervacije.add( new Rezervacija(
                                   rs.getInt("rid"),
                                   rs.getString("namena"),
                                   rs.getString("datum"),
                                   rs.getString("datumRezervisanja"),
                                   rs.getString("PocevOd"),
                                   rs.getString("Trajanje")
                                   )
                         );
            }
            for(int i=0;i<list.size();i++){
               int sid=list.get(i);
               rs = stmt.executeQuery("select * from sala where sid = "+sid+";");
               Rezervacija r=rezervacije.get(i);
               if(rs.next())r.setSala(rs.getString("naziv"));
            }
            session.setAttribute("korrez", rezervacije);
            session.setAttribute("kor", this);
            stmt.close();
            con.close();

        } catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {
                }
            }
            vrati="error";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            return vrati;
        }
        User u=(User)session.getAttribute("user");
        return u.adminuserp();
   }
    
   public String brisi(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        String vrati;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            stmt.executeUpdate("delete from rezervacija where korisnik='"+kid+"';");
            stmt.executeUpdate("delete from korisnik where kid='"+kid+"';");
            stmt.close();
            con.close();

        } catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {
                }
            }
            vrati="error";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            return vrati;
        }
        User u=(User)session.getAttribute("user");
        context.addMessage(null, new FacesMessage("Корисник успешно избрисан!"));
        return u.apregledkor();
   }

    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the ime
     */
    public String getIme() {
        return ime;
    }

    /**
     * @param ime the ime to set
     */
    public void setIme(String ime) {
        this.ime = ime;
    }

    public int getKid() {
        return kid;
    }

    /**
     * @param ime the ime to set
     */
    public void setKid(int kid) {
        this.kid = kid;
    }

    /**
     * @return the prezime
     */
    public String getPrezime() {
        return prezime;
    }

    /**
     * @param prezime the prezime to set
     */
    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the funkcija
     */
    public String getFunkcija() {
        return funkcija;
    }

    /**
     * @param funkcija the funkcija to set
     */
    public void setFunkcija(String funkcija) {
        this.funkcija = funkcija;
    }

}
